/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.ui;

import jbxml.Dictionary;

/**
 * Class representing the insets of an object.
 *
 * @author gbrown
 */
public class Insets 
    extends Object
    implements jbxml.Value
{

    public static final String TOP_KEY = "top";
    public static final String LEFT_KEY = "left";
    public static final String BOTTOM_KEY = "bottom";
    public static final String RIGHT_KEY = "right";


    public float top = 0;
    public float right = 0;
    public float bottom = 0;
    public float left = 0;


    public Insets() {
        super();
    }
    public Insets(float inset) {
        super();
        this.top = inset;
        this.right = inset;
        this.bottom = inset;
        this.left = inset;
    }
    public Insets(Dictionary<String, ?> insets) {
        super();
        if (insets == null) {
            throw new IllegalArgumentException("insets is null.");
        }
        else {
            if (insets.containsKey(TOP_KEY))
                this.top = ((Number)insets.get(TOP_KEY)).floatValue();

            if (insets.containsKey(LEFT_KEY))
                this.left = ((Number)insets.get(LEFT_KEY)).floatValue();

            if (insets.containsKey(BOTTOM_KEY))
                this.bottom = ((Number)insets.get(BOTTOM_KEY)).floatValue();

            if (insets.containsKey(RIGHT_KEY))
                this.right = ((Number)insets.get(RIGHT_KEY)).floatValue();
        }
    }
    public Insets(float top, float right, float bottom, float left) {
        super();
        this.top = top;
        this.right = right;
        this.bottom = bottom;
        this.left = left;
    }
    public Insets(Insets insets) {
        super();
        if (insets == null) {
            throw new IllegalArgumentException("insets is null.");
        }
        else {
            this.top = insets.top;
            this.right = insets.right;
            this.bottom = insets.bottom;
            this.left = insets.left;
        }
    }
    public Insets(String css) {
        super();
        this.fromString(css);
    }


    public float getTop(){
        return this.top;
    }
    public void setTop(float w){
        if (0 < w)
            this.top = w;
    }
    public float getRight(){
        return this.right;
    }
    public void setRight(float w){
        if (0 < w)
            this.right = w;
    }
    public float getBottom(){
        return this.bottom;
    }
    public void setBottom(float w){
        if (0 < w)
            this.bottom = w;
    }
    public float getLeft(){
        return this.left;
    }
    public void setLeft(float w){
        if (0 < w)
            this.left = w;
    }
    public boolean equals(Object object) {
        boolean equals = false;

        if (object instanceof Insets) {
            Insets insets = (Insets)object;
            equals = (top == insets.top
                && left == insets.left
                && bottom == insets.bottom
                && right == insets.right);
        }

        return equals;
    }
    public void fromString(String css){
        this.top = 0;
        this.right = 0;
        this.bottom = 0;
        this.left = 0;

        if (null != css){
            java.util.StringTokenizer strtok = new java.util.StringTokenizer(css," \t\r\n");
            switch (strtok.countTokens()){
            case 0:
                break;
            case 1:
                float a = Float.parseFloat(strtok.nextToken());
                this.top = a;
                this.right = a;
                this.bottom = a;
                this.left = a;
                break;
            case 2:
                float b = Float.parseFloat(strtok.nextToken());
                float c = Float.parseFloat(strtok.nextToken());
                this.top = b;
                this.right = c;
                this.bottom = b;
                this.left = c;
                break;
            case 3:
                float d = Float.parseFloat(strtok.nextToken());
                float e = Float.parseFloat(strtok.nextToken());
                float f = Float.parseFloat(strtok.nextToken());
                this.top = d;
                this.right = e;
                this.bottom = d;
                this.left = f;
                break;
            case 4:
                float g = Float.parseFloat(strtok.nextToken());
                float h = Float.parseFloat(strtok.nextToken());
                float i = Float.parseFloat(strtok.nextToken());
                float j = Float.parseFloat(strtok.nextToken());
                this.top = g;
                this.right = h;
                this.bottom = i;
                this.left = j;
                break;
            default:
                throw new IllegalArgumentException(css);
            }
        }
    }
    public String toString() {
        return (top+" "+left+" "+bottom+" "+right);
    }
}
